<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>
        Java 日期与时间 |
        
        Darklovy</title>
    <meta name="description" content>
    <meta name="keywords" content>
    <meta name="HandheldFriendly" content="True">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="shortcut icon" href="/">
    <link rel="alternate" href="/atom.xml" title="Darklovy">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。 12345第一个构造函数使用当前日期和时间来初始化对象。Date( )    第二个构造函数接收一个参数，该参数是从1970年1月1日起的毫秒数。Date(long millisec) 常用方法12345678910111213141516171819202122232425">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 日期与时间">
<meta property="og:url" content="https://www.ttxxly.top/2019/02/19/Java-日期与时间/index.html">
<meta property="og:site_name" content="Darklovy">
<meta property="og:description" content="java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。 12345第一个构造函数使用当前日期和时间来初始化对象。Date( )    第二个构造函数接收一个参数，该参数是从1970年1月1日起的毫秒数。Date(long millisec) 常用方法12345678910111213141516171819202122232425">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-02-20T06:24:19.858Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 日期与时间">
<meta name="twitter:description" content="java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。 12345第一个构造函数使用当前日期和时间来初始化对象。Date( )    第二个构造函数接收一个参数，该参数是从1970年1月1日起的毫秒数。Date(long millisec) 常用方法12345678910111213141516171819202122232425">

    <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
    <link href="//cdn.bootcss.com/node-waves/0.7.5/waves.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script>
        function setLoadingBarProgress(num) {
            document.getElementById('loading-bar').style.width = num + "%";
        }
    </script>
</head>
</html>
<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script> 
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				Darklovy
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-Home' href='/'>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-categories' href='/categories'>
								分类
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-tags' href='/tags'>
								标签
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-algorithm' href='https://book.algorithm.ttxxly.top/'>
								算法
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-python' href='https://book.python.ttxxly.top/'>
								python
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>
		
		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-Home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/categories" class="nav-categories nav">
				分类
			</a>
		
			<a href="/tags" class="nav-tags nav">
				标签
			</a>
		
			<a href="https://book.algorithm.ttxxly.top/" class="nav-algorithm nav">
				算法
			</a>
		
			<a href="https://book.python.ttxxly.top/" class="nav-python nav">
				python
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <div class='l_main'>
        <article id="post-Java-日期与时间"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>
	<h2 class="title">
  	<a href="/2019/02/19/Java-日期与时间/">
    	Java 日期与时间
    </a>
  </h2>
	<time>
	  Feb 19, 2019
	</time>
	
	</section>
	
		<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#常用方法"><span class="toc-number">1.</span> <span class="toc-text">常用方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#获取当前日期时间"><span class="toc-number">2.</span> <span class="toc-text">获取当前日期时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#日期比较"><span class="toc-number">3.</span> <span class="toc-text">日期比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用-SimpleDateFormat-格式化日期"><span class="toc-number">4.</span> <span class="toc-text">使用 SimpleDateFormat 格式化日期</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#日期和时间的格式化编码"><span class="toc-number">5.</span> <span class="toc-text">日期和时间的格式化编码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用printf格式化日期"><span class="toc-number">6.</span> <span class="toc-text">使用printf格式化日期</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解析字符串为时间"><span class="toc-number">7.</span> <span class="toc-text">解析字符串为时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#测量时间"><span class="toc-number">8.</span> <span class="toc-text">测量时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#利用-Calendar-类在日期做加减"><span class="toc-number">9.</span> <span class="toc-text">利用 Calendar 类在日期做加减</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建一个代表系统当前日期的Calendar对象"><span class="toc-number">10.</span> <span class="toc-text">创建一个代表系统当前日期的Calendar对象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#创建一个指定日期的Calendar对象"><span class="toc-number">10.1.</span> <span class="toc-text">创建一个指定日期的Calendar对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Calendar类对象字段类型"><span class="toc-number">10.2.</span> <span class="toc-text">Calendar类对象字段类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Calendar类对象信息的设置"><span class="toc-number">10.3.</span> <span class="toc-text">Calendar类对象信息的设置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Calendar类对象信息的获得"><span class="toc-number">10.4.</span> <span class="toc-text">Calendar类对象信息的获得</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#GregorianCalendar类"><span class="toc-number">10.5.</span> <span class="toc-text">GregorianCalendar类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#实例"><span class="toc-number">10.6.</span> <span class="toc-text">实例</span></a></li></ol></li></ol></section>
	
	<section class="article typo">
  	<div class="article-entry" itemprop="articleBody">
    	<p>java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">第一个构造函数使用当前日期和时间来初始化对象。</span><br><span class="line">Date( )</span><br><span class="line">    </span><br><span class="line">第二个构造函数接收一个参数，该参数是从<span class="number">1970</span>年<span class="number">1</span>月<span class="number">1</span>日起的毫秒数。</span><br><span class="line">Date(<span class="keyword">long</span> millisec)</span><br></pre></td></tr></table></figure>
<h3 id="常用方法"><a href="#常用方法" class="headerlink" title="常用方法"></a>常用方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">after</span><span class="params">(Date date)</span></span></span><br><span class="line"><span class="function">若当调用此方法的Date对象在指定日期之后返回<span class="keyword">true</span>,否则返回<span class="keyword">false</span>。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">before</span><span class="params">(Date date)</span></span></span><br><span class="line"><span class="function">若当调用此方法的Date对象在指定日期之前返回<span class="keyword">true</span>,否则返回<span class="keyword">false</span>。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">Object <span class="title">clone</span><span class="params">( )</span></span></span><br><span class="line"><span class="function">返回此对象的副本。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(Date date)</span></span></span><br><span class="line"><span class="function">比较当调用此方法的Date对象和指定日期。两者相等时候返回0。</span></span><br><span class="line"><span class="function">调用对象在指定日期之前则返回负数。调用对象在指定日期之后则返回正数。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(Object obj)</span></span></span><br><span class="line"><span class="function">若obj是Date类型则操作等同于<span class="title">compareTo</span><span class="params">(Date)</span> 。否则它抛出ClassCastException。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object date)</span></span></span><br><span class="line"><span class="function">当调用此方法的Date对象和指定日期相等时候返回<span class="keyword">true</span>,否则返回<span class="keyword">false</span>。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="title">getTime</span><span class="params">( )</span></span></span><br><span class="line"><span class="function">返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">hashCode</span><span class="params">( )</span></span></span><br><span class="line"><span class="function">返回此对象的哈希码值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setTime</span><span class="params">(<span class="keyword">long</span> time)</span></span></span><br><span class="line"><span class="function">用自1970年1月1日00:00:00 GMT以后time毫秒数设置时间和日期。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">String <span class="title">toString</span><span class="params">( )</span></span></span><br><span class="line"><span class="function">把此 Date 对象转换为以下形式的 String： dow mon dd hh:mm:ss zzz yyyy </span></span><br><span class="line"><span class="function">其中： dow 是一周中的某一天 <span class="params">(Sun, Mon, Tue, Wed, Thu, Fri, Sat)</span>。</span></span><br></pre></td></tr></table></figure>
<h3 id="获取当前日期时间"><a href="#获取当前日期时间" class="headerlink" title="获取当前日期时间"></a>获取当前日期时间</h3><p>Java中获取当前日期和时间很简单，使用 Date 对象的 toString() 方法来打印当前日期和时间，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DateDemo</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">       <span class="comment">// 初始化 Date 对象</span></span><br><span class="line">       Date date = <span class="keyword">new</span> Date();</span><br><span class="line">        </span><br><span class="line">       <span class="comment">// 使用 toString() 函数显示日期时间</span></span><br><span class="line">       System.out.println(date.toString());</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">编译结果如下：</span><br><span class="line">Mon May <span class="number">04</span> <span class="number">09</span>:<span class="number">51</span>:<span class="number">52</span> CDT <span class="number">2013</span></span><br></pre></td></tr></table></figure>
<h3 id="日期比较"><a href="#日期比较" class="headerlink" title="日期比较"></a>日期比较</h3><p>Java使用以下三种方法来比较两个日期：</p>
<ul>
<li>使用 getTime() 方法获取两个日期（自1970年1月1日经历的毫秒数值），然后比较这两个值。</li>
<li>使用方法 before()，after() 和 equals()。例如，一个月的12号比18号早，则 new Date(99, 2, 12).before(new Date (99, 2, 18)) 返回true。</li>
<li>使用 compareTo() 方法，它是由 Comparable 接口定义的，Date 类实现了这个接口。</li>
</ul>
<h3 id="使用-SimpleDateFormat-格式化日期"><a href="#使用-SimpleDateFormat-格式化日期" class="headerlink" title="使用 SimpleDateFormat 格式化日期"></a>使用 SimpleDateFormat 格式化日期</h3><p>SimpleDateFormat 是一个以语言环境敏感的方式来格式化和分析日期的类。SimpleDateFormat 允许你选择任何用户自定义日期时间格式来运行。例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"><span class="keyword">import</span> java.text.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DateDemo</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">      Date dNow = <span class="keyword">new</span> Date( );</span><br><span class="line">      SimpleDateFormat ft = </span><br><span class="line">      <span class="keyword">new</span> SimpleDateFormat (<span class="string">"E yyyy.MM.dd 'at' hh:mm:ss a zzz"</span>);</span><br><span class="line"></span><br><span class="line">      System.out.println(<span class="string">"Current Date: "</span> + ft.format(dNow));</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">结果：</span><br><span class="line">Current Date: Wed <span class="number">2016.11</span>.09 at <span class="number">08</span>:<span class="number">23</span>:<span class="number">19</span> AM UTC</span><br></pre></td></tr></table></figure>
<h3 id="日期和时间的格式化编码"><a href="#日期和时间的格式化编码" class="headerlink" title="日期和时间的格式化编码"></a>日期和时间的格式化编码</h3><p>时间模式字符串用来指定时间格式。在此模式中，所有的 ASCII 字母被保留为模式字母，定义如下：</p>
<table>
<thead>
<tr>
<th><strong>字母</strong></th>
<th><strong>描述</strong></th>
<th><strong>示例</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>G</td>
<td>纪元标记</td>
<td>AD</td>
</tr>
<tr>
<td>y</td>
<td>四位年份</td>
<td>2001</td>
</tr>
<tr>
<td>M</td>
<td>月份</td>
<td>July or 07</td>
</tr>
<tr>
<td>d</td>
<td>一个月的日期</td>
<td>10</td>
</tr>
<tr>
<td>h</td>
<td>A.M./P.M. (1~12)格式小时</td>
<td>12</td>
</tr>
<tr>
<td>H</td>
<td>一天中的小时 (0~23)</td>
<td>22</td>
</tr>
<tr>
<td>m</td>
<td>分钟数</td>
<td>30</td>
</tr>
<tr>
<td>s</td>
<td>秒数</td>
<td>55</td>
</tr>
<tr>
<td>S</td>
<td>毫秒数</td>
<td>234</td>
</tr>
<tr>
<td>E</td>
<td>星期几</td>
<td>Tuesday</td>
</tr>
<tr>
<td>D</td>
<td>一年中的日子</td>
<td>360</td>
</tr>
<tr>
<td>F</td>
<td>一个月中第几周的周几</td>
<td>2 (second Wed. in July)</td>
</tr>
<tr>
<td>w</td>
<td>一年中第几周</td>
<td>40</td>
</tr>
<tr>
<td>W</td>
<td>一个月中第几周</td>
<td>1</td>
</tr>
<tr>
<td>a</td>
<td>A.M./P.M. 标记</td>
<td>PM</td>
</tr>
<tr>
<td>k</td>
<td>一天中的小时(1~24)</td>
<td>24</td>
</tr>
<tr>
<td>K</td>
<td>A.M./P.M. (0~11)格式小时</td>
<td>10</td>
</tr>
<tr>
<td>z</td>
<td>时区</td>
<td>Eastern Standard Time</td>
</tr>
<tr>
<td>‘</td>
<td>文字定界符</td>
<td>Delimiter</td>
</tr>
<tr>
<td>“</td>
<td>单引号</td>
<td>`</td>
</tr>
</tbody>
</table>
<h3 id="使用printf格式化日期"><a href="#使用printf格式化日期" class="headerlink" title="使用printf格式化日期"></a>使用printf格式化日期</h3><p>printf 方法可以很轻松地格式化时间和日期。使用两个字母格式，它以 %t 开头并且以下面表格中的一个字母结尾。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DateDemo</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">     <span class="comment">// 初始化 Date 对象</span></span><br><span class="line">     Date date = <span class="keyword">new</span> Date();</span><br><span class="line"> </span><br><span class="line">     <span class="comment">//c的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"全部日期和时间信息：%tc%n"</span>,date);          </span><br><span class="line">    <span class="comment">//f的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"年-月-日格式：%tF%n"</span>,date);  </span><br><span class="line">    <span class="comment">//d的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"月/日/年格式：%tD%n"</span>,date);  </span><br><span class="line">    <span class="comment">//r的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"HH:MM:SS PM格式（12时制）：%tr%n"</span>,date);  </span><br><span class="line">    <span class="comment">//t的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"HH:MM:SS格式（24时制）：%tT%n"</span>,date);  </span><br><span class="line">    <span class="comment">//R的使用  </span></span><br><span class="line">    System.out.printf(<span class="string">"HH:MM格式（24时制）：%tR"</span>,date);  </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">结果：</span><br><span class="line"></span><br><span class="line">全部日期和时间信息：星期一 九月 <span class="number">10</span> <span class="number">10</span>:<span class="number">43</span>:<span class="number">36</span> CST <span class="number">2012</span>  </span><br><span class="line">年-月-日格式：<span class="number">2012</span>-<span class="number">09</span>-<span class="number">10</span>  </span><br><span class="line">月/日/年格式：<span class="number">09</span>/<span class="number">10</span>/<span class="number">12</span>  </span><br><span class="line">HH:MM:SS PM格式（<span class="number">12</span>时制）：<span class="number">10</span>:<span class="number">43</span>:<span class="number">36</span> 上午  </span><br><span class="line">HH:MM:SS格式（<span class="number">24</span>时制）：<span class="number">10</span>:<span class="number">43</span>:<span class="number">36</span>  </span><br><span class="line">HH:MM格式（<span class="number">24</span>时制）：<span class="number">10</span>:<span class="number">43</span></span><br></pre></td></tr></table></figure>
<h3 id="解析字符串为时间"><a href="#解析字符串为时间" class="headerlink" title="解析字符串为时间"></a>解析字符串为时间</h3><p>SimpleDateFormat 类有一些附加的方法，特别是parse()，它试图按照给定的SimpleDateFormat 对象的格式化存储来解析字符串。例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"><span class="keyword">import</span> java.text.*;</span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DateDemo</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">      SimpleDateFormat ft = <span class="keyword">new</span> SimpleDateFormat (<span class="string">"yyyy-MM-dd"</span>); </span><br><span class="line"> </span><br><span class="line">      String input = args.length == <span class="number">0</span> ? <span class="string">"1818-11-11"</span> : args[<span class="number">0</span>]; </span><br><span class="line"> </span><br><span class="line">      System.out.print(input + <span class="string">" Parses as "</span>); </span><br><span class="line"> </span><br><span class="line">      Date t; </span><br><span class="line"> </span><br><span class="line">      <span class="keyword">try</span> &#123; </span><br><span class="line">          t = ft.parse(input); </span><br><span class="line">          System.out.println(t); </span><br><span class="line">      &#125; <span class="keyword">catch</span> (ParseException e) &#123; </span><br><span class="line">          System.out.println(<span class="string">"Unparseable using "</span> + ft); </span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">result:</span><br><span class="line"></span><br><span class="line">$ java DateDemo</span><br><span class="line"><span class="number">1818</span>-<span class="number">11</span>-<span class="number">11</span> Parses as Wed Nov <span class="number">11</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> GMT <span class="number">1818</span></span><br><span class="line">$ java DateDemo <span class="number">2007</span>-<span class="number">12</span>-<span class="number">01</span></span><br><span class="line"><span class="number">2007</span>-<span class="number">12</span>-<span class="number">01</span> Parses as Sat Dec <span class="number">01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> GMT <span class="number">2007</span></span><br></pre></td></tr></table></figure>
<h3 id="测量时间"><a href="#测量时间" class="headerlink" title="测量时间"></a>测量时间</h3><p>下面的一个例子表明如何测量时间间隔（以毫秒为单位）：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DiffDemo</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">         <span class="keyword">long</span> start = System.currentTimeMillis( );</span><br><span class="line">         System.out.println(<span class="keyword">new</span> Date( ) + <span class="string">"\n"</span>);</span><br><span class="line">         Thread.sleep(<span class="number">5</span>*<span class="number">60</span>*<span class="number">10</span>);</span><br><span class="line">         System.out.println(<span class="keyword">new</span> Date( ) + <span class="string">"\n"</span>);</span><br><span class="line">         <span class="keyword">long</span> end = System.currentTimeMillis( );</span><br><span class="line">         <span class="keyword">long</span> diff = end - start;</span><br><span class="line">         System.out.println(<span class="string">"Difference is : "</span> + diff);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">         System.out.println(<span class="string">"Got an exception!"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">result:</span><br><span class="line"></span><br><span class="line">Fri Jan <span class="number">08</span> <span class="number">09</span>:<span class="number">48</span>:<span class="number">47</span> CST <span class="number">2016</span></span><br><span class="line"></span><br><span class="line">Fri Jan <span class="number">08</span> <span class="number">09</span>:<span class="number">48</span>:<span class="number">50</span> CST <span class="number">2016</span></span><br><span class="line"></span><br><span class="line">Difference is : <span class="number">3019</span></span><br></pre></td></tr></table></figure>
<h3 id="利用-Calendar-类在日期做加减"><a href="#利用-Calendar-类在日期做加减" class="headerlink" title="利用 Calendar 类在日期做加减"></a>利用 Calendar 类在日期做加减</h3><p>我们现在已经能够格式化并创建一个日期对象了，但是我们如何才能设置和获取日期数据的特定部分呢，比如说小时，日，或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用Calendar 类。</p>
<p>Calendar类的功能要比Date类强大很多，而且在实现方式上也比Date类要复杂一些。</p>
<p>Calendar类是一个抽象类，在实际使用时实现特定的子类的对象，创建对象的过程对程序员来说是透明的，只需要使用getInstance方法创建即可。</p>
<h3 id="创建一个代表系统当前日期的Calendar对象"><a href="#创建一个代表系统当前日期的Calendar对象" class="headerlink" title="创建一个代表系统当前日期的Calendar对象"></a>创建一个代表系统当前日期的Calendar对象</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Calendar c = Calendar.getInstance();//默认是当前日期</span><br></pre></td></tr></table></figure>
<h4 id="创建一个指定日期的Calendar对象"><a href="#创建一个指定日期的Calendar对象" class="headerlink" title="创建一个指定日期的Calendar对象"></a>创建一个指定日期的Calendar对象</h4><p>使用Calendar类代表特定的时间，需要首先创建一个Calendar的对象，然后再设定该对象中的年月日参数来完成。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">//创建一个代表2009年6月12日的Calendar对象</span><br><span class="line">Calendar c1 = Calendar.getInstance();</span><br><span class="line">c1.set(2009, 6 - 1, 12);</span><br></pre></td></tr></table></figure>
<h4 id="Calendar类对象字段类型"><a href="#Calendar类对象字段类型" class="headerlink" title="Calendar类对象字段类型"></a>Calendar类对象字段类型</h4><p>Calendar类中用以下这些常量表示不同的意义，jdk内的很多类其实都是采用的这种思想</p>
<table>
<thead>
<tr>
<th>常量</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>Calendar.YEAR</td>
<td>年份</td>
</tr>
<tr>
<td>Calendar.MONTH</td>
<td>月份</td>
</tr>
<tr>
<td>Calendar.DATE</td>
<td>日期</td>
</tr>
<tr>
<td>Calendar.DAY_OF_MONTH</td>
<td>日期，和上面的字段意义完全相同</td>
</tr>
<tr>
<td>Calendar.HOUR</td>
<td>12小时制的小时</td>
</tr>
<tr>
<td>Calendar.HOUR_OF_DAY</td>
<td>24小时制的小时</td>
</tr>
<tr>
<td>Calendar.MINUTE</td>
<td>分钟</td>
</tr>
<tr>
<td>Calendar.SECOND</td>
<td>秒</td>
</tr>
<tr>
<td>Calendar.DAY_OF_WEEK</td>
<td>星期几</td>
</tr>
</tbody>
</table>
<h4 id="Calendar类对象信息的设置"><a href="#Calendar类对象信息的设置" class="headerlink" title="Calendar类对象信息的设置"></a>Calendar类对象信息的设置</h4><p><strong>Set设置</strong></p>
<p>如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Calendar c1 = Calendar.getInstance();</span><br></pre></td></tr></table></figure>
<p>调用：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">public final void set(int year,int month,int date)</span><br><span class="line">c1.set(2009, 6 - 1, 12);//把Calendar对象c1的年月日分别设这为：2009、6、12</span><br></pre></td></tr></table></figure>
<p>利用字段类型设置</p>
<p>如果只设定某个字段，例如日期的值，则可以使用如下set方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">public void set(int field,int value)</span><br></pre></td></tr></table></figure>
<p>把 c1对象代表的日期设置为10号，其它所有的数值会被重新计算</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c1.set(Calendar.DATE,10);</span><br></pre></td></tr></table></figure>
<p>把c1对象代表的年份设置为2008年，其他的所有数值会被重新计算</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c1.set(Calendar.YEAR,2008);</span><br></pre></td></tr></table></figure>
<p>其他字段属性set的意义以此类推</p>
<p><strong>Add设置</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Calendar c1 = Calendar.getInstance();</span><br></pre></td></tr></table></figure>
<p>把c1对象的日期加上10，也就是c1也就表示为10天后的日期，其它所有的数值会被重新计算</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c1.add(Calendar.DATE, 10);</span><br></pre></td></tr></table></figure>
<p>把c1对象的日期减去10，也就是c1也就表示为10天前的日期，其它所有的数值会被重新计算</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c1.add(Calendar.DATE, -10);</span><br></pre></td></tr></table></figure>
<p>其他字段属性的add的意义以此类推</p>
<h4 id="Calendar类对象信息的获得"><a href="#Calendar类对象信息的获得" class="headerlink" title="Calendar类对象信息的获得"></a>Calendar类对象信息的获得</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Calendar c1 = Calendar.getInstance();</span><br><span class="line"><span class="comment">// 获得年份</span></span><br><span class="line"><span class="keyword">int</span> year = c1.get(Calendar.YEAR);</span><br><span class="line"><span class="comment">// 获得月份</span></span><br><span class="line"><span class="keyword">int</span> month = c1.get(Calendar.MONTH) + <span class="number">1</span>;</span><br><span class="line"><span class="comment">// 获得日期</span></span><br><span class="line"><span class="keyword">int</span> date = c1.get(Calendar.DATE);</span><br><span class="line"><span class="comment">// 获得小时</span></span><br><span class="line"><span class="keyword">int</span> hour = c1.get(Calendar.HOUR_OF_DAY);</span><br><span class="line"><span class="comment">// 获得分钟</span></span><br><span class="line"><span class="keyword">int</span> minute = c1.get(Calendar.MINUTE);</span><br><span class="line"><span class="comment">// 获得秒</span></span><br><span class="line"><span class="keyword">int</span> second = c1.get(Calendar.SECOND);</span><br><span class="line"><span class="comment">// 获得星期几（注意（这个与Date类是不同的）：1代表星期日、2代表星期1、3代表星期二，以此类推）</span></span><br><span class="line"><span class="keyword">int</span> day = c1.get(Calendar.DAY_OF_WEEK);</span><br></pre></td></tr></table></figure>
<h4 id="GregorianCalendar类"><a href="#GregorianCalendar类" class="headerlink" title="GregorianCalendar类"></a>GregorianCalendar类</h4><p>Calendar类实现了公历日历，GregorianCalendar是Calendar类的一个具体实现。</p>
<p>Calendar 的getInstance（）方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段：AD和BC。这是代表公历定义的两个时代。</p>
<p>下面列出GregorianCalendar对象的几个构造方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">GregorianCalendar() </span><br><span class="line">在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。</span><br><span class="line"></span><br><span class="line">GregorianCalendar(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date) </span><br><span class="line">在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar</span><br><span class="line"></span><br><span class="line">GregorianCalendar(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date, <span class="keyword">int</span> hour, <span class="keyword">int</span> minute) </span><br><span class="line">为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。</span><br><span class="line"></span><br><span class="line">GregorianCalendar(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date, <span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second) </span><br><span class="line">为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。</span><br><span class="line"></span><br><span class="line">GregorianCalendar(Locale aLocale) </span><br><span class="line">在具有给定语言环境的默认时区内构造一个基于当前时间的 GregorianCalendar</span><br><span class="line"></span><br><span class="line">GregorianCalendar(TimeZone zone) </span><br><span class="line">在具有默认语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。</span><br><span class="line"></span><br><span class="line">GregorianCalendar(TimeZone zone, Locale aLocale) </span><br><span class="line">在具有给定语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。</span><br></pre></td></tr></table></figure>
<p>这里是GregorianCalendar 类提供的一些有用的方法列表：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> field, <span class="keyword">int</span> amount)</span> </span></span><br><span class="line"><span class="function">根据日历规则，将指定的（有符号的）时间量添加到给定的日历字段中。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">computeFields</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">转换UTC毫秒值为时间域值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">computeTime</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">覆盖Calendar ，转换时间域值为UTC毫秒值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span></span><br><span class="line"><span class="function">比较此 GregorianCalendar 与指定的 Object。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">get</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">获取指定字段的时间值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getActualMaximum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">返回当前日期，给定字段的最大值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getActualMinimum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">返回当前日期，给定字段的最小值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getGreatestMinimum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function"> 返回此 GregorianCalendar 实例给定日历字段的最高的最小值。</span></span><br><span class="line"><span class="function"> </span></span><br><span class="line"><span class="function">Date <span class="title">getGregorianChange</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">获得格里高利历的更改日期。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getLeastMaximum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">返回此 GregorianCalendar 实例给定日历字段的最低的最大值</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getMaximum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">返回此 GregorianCalendar 实例的给定日历字段的最大值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">Date <span class="title">getTime</span><span class="params">()</span></span></span><br><span class="line"><span class="function">获取日历当前时间。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="title">getTimeInMillis</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">获取用长整型表示的日历的当前时间</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">TimeZone <span class="title">getTimeZone</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">获取时区。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getMinimum</span><span class="params">(<span class="keyword">int</span> field)</span> </span></span><br><span class="line"><span class="function">返回给定字段的最小值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">重写hashCode.</span></span><br><span class="line"><span class="function">    </span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isLeapYear</span><span class="params">(<span class="keyword">int</span> year)</span></span></span><br><span class="line"><span class="function">确定给定的年份是否为闰年。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">roll</span><span class="params">(<span class="keyword">int</span> field, <span class="keyword">boolean</span> up)</span> </span></span><br><span class="line"><span class="function">在给定的时间字段上添加或减去（上/下）单个时间单元，不更改更大的字段。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> field, <span class="keyword">int</span> value)</span> </span></span><br><span class="line"><span class="function">用给定的值设置时间字段。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date)</span> </span></span><br><span class="line"><span class="function">设置年、月、日的值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date, <span class="keyword">int</span> hour, <span class="keyword">int</span> minute)</span> </span></span><br><span class="line"><span class="function">设置年、月、日、小时、分钟的值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> date, <span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second)</span> </span></span><br><span class="line"><span class="function">设置年、月、日、小时、分钟、秒的值。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setGregorianChange</span><span class="params">(Date date)</span> </span></span><br><span class="line"><span class="function">设置 GregorianCalendar 的更改日期。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setTime</span><span class="params">(Date date)</span> </span></span><br><span class="line"><span class="function">用给定的日期设置Calendar的当前时间。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setTimeInMillis</span><span class="params">(<span class="keyword">long</span> millis)</span> </span></span><br><span class="line"><span class="function">用给定的<span class="keyword">long</span>型毫秒数设置Calendar的当前时间。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setTimeZone</span><span class="params">(TimeZone value)</span> </span></span><br><span class="line"><span class="function">用给定时区值设置当前时区。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">String <span class="title">toString</span><span class="params">()</span> </span></span><br><span class="line"><span class="function">返回代表日历的字符串。</span></span><br></pre></td></tr></table></figure>
<h4 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GregorianCalendarDemo</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">      String months[] = &#123;</span><br><span class="line">      <span class="string">"Jan"</span>, <span class="string">"Feb"</span>, <span class="string">"Mar"</span>, <span class="string">"Apr"</span>,</span><br><span class="line">      <span class="string">"May"</span>, <span class="string">"Jun"</span>, <span class="string">"Jul"</span>, <span class="string">"Aug"</span>,</span><br><span class="line">      <span class="string">"Sep"</span>, <span class="string">"Oct"</span>, <span class="string">"Nov"</span>, <span class="string">"Dec"</span>&#125;;</span><br><span class="line">      </span><br><span class="line">      <span class="keyword">int</span> year;</span><br><span class="line">      <span class="comment">// 初始化 Gregorian 日历</span></span><br><span class="line">      <span class="comment">// 使用当前时间和日期</span></span><br><span class="line">      <span class="comment">// 默认为本地时间和时区</span></span><br><span class="line">      GregorianCalendar gcalendar = <span class="keyword">new</span> GregorianCalendar();</span><br><span class="line">      <span class="comment">// 显示当前时间和日期的信息</span></span><br><span class="line">      System.out.print(<span class="string">"Date: "</span>);</span><br><span class="line">      System.out.print(months[gcalendar.get(Calendar.MONTH)]);</span><br><span class="line">      System.out.print(<span class="string">" "</span> + gcalendar.get(Calendar.DATE) + <span class="string">" "</span>);</span><br><span class="line">      System.out.println(year = gcalendar.get(Calendar.YEAR));</span><br><span class="line">      System.out.print(<span class="string">"Time: "</span>);</span><br><span class="line">      System.out.print(gcalendar.get(Calendar.HOUR) + <span class="string">":"</span>);</span><br><span class="line">      System.out.print(gcalendar.get(Calendar.MINUTE) + <span class="string">":"</span>);</span><br><span class="line">      System.out.println(gcalendar.get(Calendar.SECOND));</span><br><span class="line">      </span><br><span class="line">      <span class="comment">// 测试当前年份是否为闰年</span></span><br><span class="line">      <span class="keyword">if</span>(gcalendar.isLeapYear(year)) &#123;</span><br><span class="line">         System.out.println(<span class="string">"当前年份是闰年"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span> &#123;</span><br><span class="line">         System.out.println(<span class="string">"当前年份不是闰年"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  	</div>
	  
	  <div class="article-tags tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
		
	
		<div class="art-item-footer">
				
					<span class="art-item-left"><i class="icon icon-chevron-thin-left"></i>prev：<a href="/2019/02/20/Java-Class-类与-Java-反射/" rel="prev"  title="Java Class 类与 Java 反射">
						Java Class 类与 Java 反射 
					</a></span>
				
				
					<span class="art-item-right">next：<a href="/2019/02/18/Java-数组排序/" rel="next"  title="Java 数组排序">
						Java 数组排序
					</a><i class="icon icon-chevron-thin-right"></i></span>
				
		</div>
	
	</section>
	
</article>
<script>
	window.subData = {
		title: 'Java 日期与时间',
		tools: true
	}
</script>

      </div>
      <aside class='l_side'>
        
  <section class='m_widget links'>
<div class='header'>Links</div>
<div class='content'>
    <ul class="entry">
    
        <li><a class="flat-box" target="_blank" href="https://iiong.com/">
            <div class='name'>淮城一只猫</div>
        </a></li>
    
    </ul>
</div>
</section>

  <section class='m_widget categories'>
<div class='header'>Categories</div>
<div class='content'>
    
    <ul class="entry">
    
        <li><a class="flat-box" href="/categories/Android基础/"><div class='name'>Android基础</div><div class='badget'>1</div></a></li>
    
    </ul>
    
</div>
</section>

  
<div class="m_widget tagcloud">
    <div class="header">Tags</div>
    <div class='content'>
        <a href="/tags/Android/" style="font-size: 14px; color: #808080">Android</a> <a href="/tags/HTML/" style="font-size: 14px; color: #808080">HTML</a> <a href="/tags/Java/" style="font-size: 20px; color: #000">Java</a> <a href="/tags/Python/" style="font-size: 14px; color: #808080">Python</a>
    </div>
</div>



      </aside>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/stkevintan" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://twitter.com/kevinsfork" class="social twitter"
          target="_blank" rel="external">
          <span class="icon icon-twitter"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a> designed by <a href="http://keyin.me/" target="_blank">Kevin Tan</a>.</div>
  
</footer>


  <script>setLoadingBarProgress(80);</script>
  

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
<script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
<script src="/js/jquery.fitvids.js"></script>
<script>
	var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
	var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
	var ALGOLIA_API_KEY = "";
	var ALGOLIA_APP_ID = "";
	var ALGOLIA_INDEX_NAME = "";
  var AZURE_SERVICE_NAME = "";
  var AZURE_INDEX_NAME = "";
  var AZURE_QUERY_KEY = "";
  var BAIDU_API_ID = "";
  var SEARCH_SERVICE = "hexo";
  var ROOT = "/"||"/";
  if(!ROOT.endsWith('/'))ROOT += '/';
</script>
<script src="/js/search.js"></script>
<script src="/js/app.js"></script>


  <script>setLoadingBarProgress(100);</script>
</body>
</html>
